import json

import jwt
from django.core.paginator import Paginator
from django.forms import model_to_dict
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods

from .models import *
from django.http import JsonResponse

@csrf_exempt
@require_http_methods(['POST'])
# 登录
def login(request):
    data=json.loads(request.body)
    username=data['username']
    password=data['password']
    use=User.objects.get(username=username)
    # 根据用户名、密码使用PyJWT签发token
    pay={'id':use.id,'username':use.username,'password':use.password}
    mi='123456'
    token=jwt.encode(pay,mi)
    return JsonResponse({
        'msg':"登陆成功",
        'token':token
    })


@csrf_exempt
@require_http_methods(['GET'])
# 设备‌分类查询接口
def chaxun_sbfenlei(request):
    data=SheBeiFenLei.objects.all()
    list=[]
    for i in data:
        list.append(model_to_dict(i))
    return JsonResponse({
        'msg':"查询成功",
        'data':list
    })


@csrf_exempt
@require_http_methods(['DELETE'])
# 删除前校验是否有未归还的借用记录，有则不能删除，给出提示
def shebei_delete(request,id):
    if JieYongJiLu.objects.filter(shebeiId_id=id).exists():
        return JsonResponse({
            'msg':"不能删除"
        })

    else:
        SheBeiXinXi.objects.filter(id=id).delete()
        return JsonResponse({
            'msg': "删除成功"
        })


@csrf_exempt
@require_http_methods(['GET'])
# 支持分页和条件查询（和前端需求条件对应）
def getlist(request):
    data = SheBeiXinXi.objects.all()
    # 条件查询
    shebeiname=request.GET.get('shebeiname','')
    fenleiId_id=request.GET.get('fenleiId_id','')
    # 根据设备名称查询
    if shebeiname:
        data=data.filter(shebeiname=shebeiname)
    # 根据设备分类ID查询
    if fenleiId_id:
        data=data.filter(fenleiId_id=fenleiId_id)
    # 分页
    page=request.GET.get('page',1)
    size=request.GET.get('size')or 10
    fenye=Paginator(data,size)
    data=fenye.get_page(page)
    list = []
    for i in data:
        list.append(model_to_dict(i))
    return JsonResponse({
        'msg': "查询成功",
        'data': list
    })


@csrf_exempt
@require_http_methods(['POST'])
# 添加功能
def tianjia(request):
    data=json.loads(request.body)
    shebeiname=data['shebeiname']
    cichanbianhao = data['cichanbianhao']
    fenleiId_id = data['fenleiId_id']
    gzpprice = data['gzpprice']
    num = data['num']
    status = data['status']

    a=SheBeiXinXi.objects.create(
        shebeiname=shebeiname,
        cichanbianhao=cichanbianhao,
        fenleiId_id=fenleiId_id,
        gzpprice=gzpprice,
        num=num,
        status=status
    )
    return JsonResponse({
        'msg':"添加成功",
        'data':model_to_dict(a)
    })

@csrf_exempt
@require_http_methods(['PUT'])
# 修改
def xiugai_sheibei(request,id):
    a=SheBeiXinXi.objects.get(id=id)
    data=json.loads(request.body)
    shebeiname = data['shebeiname']
    cichanbianhao = data['cichanbianhao']
    fenleiId_id = data['fenleiId_id']
    gzpprice = data['gzpprice']
    num = data['num']
    status = data['status']

    a.shebeiname=shebeiname
    a.cichanbianhao=cichanbianhao
    a.fenleiId_id=fenleiId_id
    a.gzpprice=gzpprice
    a.num=num
    a.status=status
    a.save()
    return JsonResponse({
        'msg':"修改成功",
        'data':model_to_dict(a)
    })


@csrf_exempt
@require_http_methods(['GET'])
# 支持分页和条件查询（和前端需求条件对应）
def getlist_jieyong(request):
    data = JieYongJiLu.objects.all()
    # 条件查询
    shebeiId_id=request.GET.get('shebeiId_id','')
    jieyongren=request.GET.get('jieyongren','')
    if shebeiId_id:
        data=data.filter(shebeiId_id=shebeiId_id)
    if jieyongren:
        data=data.filter(jieyongren=jieyongren)
    # 分页
    page=request.GET.get('page',1)
    size=request.GET.get('size')or 10
    fenye=Paginator(data,size)
    data=fenye.get_page(page)
    list = []
    for i in data:
        list.append(model_to_dict(i))
    return JsonResponse({
        'msg': "查询成功",
        'data': list
    })

@csrf_exempt
@require_http_methods(['POST'])
def jieyongadd(request):

    data=json.loads(request.body)
    shebeiId_id=data['shebeiId_id']
    jieyongNum=data['jieyongNum']
    jieyongren=data['jieyongren']
    jieyongtime=data['jieyongtime']
    yjghtime=data['yjghtime']
    sjghtime=data['sjghtime']

    if SheBeiXinXi.objects.filter(num=0).exists():
        return JsonResponse({
            'msg':"库存不足"
        })

    a=JieYongJiLu.objects.create(
        shebeiId_id=shebeiId_id,
        jieyongNum=jieyongNum,
        jieyongren=jieyongren,
        jieyongtime=jieyongtime,
        yjghtime=yjghtime,
        sjghtime=sjghtime
    )
    return JsonResponse({
        'msg':"预约成功",
        'data':model_to_dict(a)
    })


@csrf_exempt
@require_http_methods(['GET'])

def getlist_id(request,id):
    data = SheBeiXinXi.objects.filter(id=id).all()

    list = []
    for i in data:
        list.append(model_to_dict(i))
    return JsonResponse({
        'msg': "查询成功",
        'data': list
    })
# Create your views here.
